# 使用Node.js官方镜像作为基础镜像
FROM node:18.17.0-alpine as builder

# 设置工作目录
WORKDIR /app

# 复制package.json和package-lock.json(如果存在)
COPY package.json package-lock.json* ./

# 安装依赖
RUN npm install --registry=https://registry.npmmirror.com

RUN npm install -g @vercel/ncc

# 复制源代码
COPY . .

# 构建应用
RUN npm run build

# 构建应用2
RUN ncc build dist/main.js -o dist2

# 使用多阶段构建，创建一个更小的运行时镜像
FROM oven/bun:1-slim

WORKDIR /app

# 从构建阶段复制必要的文件
COPY --from=builder /app/dist2 ./dist2
# COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./

# 设置环境变量
ENV PORT=3001

# 暴露应用端口
EXPOSE ${PORT}

# 运行应用
CMD ["bun", "./dist2/index.js"]
